package com.emns.web.shijie;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.emns.service.shijie.ShijieCommentService;

@Controller
@RequestMapping(value = "/shijie_comment")
public class ShijieCommentController {

	@Autowired
	ShijieCommentService sjcs;

	// @RequestMapping(value = "/addShijie", method = RequestMethod.POST)
	// public String add(Model model, ShiJie shijie, int flanmu, int[] lanmu) {
	// shijie.setPublishdate(new Date());
	// shijie.setReadcount(0);
	// shijie.setPlaycount(0);
	// shijie.setDownloadcount(0);
	// shijie.setShijieId(sjs.insert(shijie));
	// for (int i = 0; i < lanmu.length; i++) {
	// LanmuZiyuan lmzy = new LanmuZiyuan();
	// lmzy.setFirstLanmuId(flanmu);
	// lmzy.setSecondLanmuId(lanmu[i]);
	// lmzy.setZiyuanId(shijie.getShijieId());
	// lmzy.setFlag("2");
	// sjs.addLanmuZiyuan(lmzy);
	// }
	// return "redirect:/shijie_add";
	// }
	//
	//
	// @RequestMapping(value = "/update", method = RequestMethod.POST)
	// public String update(Model model, ShiJie shijie, int flanmu, int[] lanmu)
	// {
	// sjs.update(shijie);
	// sjs.delLanmuZiyuanById(shijie.getShijieId());
	// for (int i = 0; i < lanmu.length; i++) {
	// LanmuZiyuan lmzy = new LanmuZiyuan();
	// lmzy.setFirstLanmuId(flanmu);
	// lmzy.setSecondLanmuId(lanmu[i]);
	// lmzy.setZiyuanId(shijie.getShijieId());
	// lmzy.setFlag("2");
	// sjs.addLanmuZiyuan(lmzy);
	// }
	// return "redirect:/shijie/query";
	// }

	@RequestMapping(value = "/query")
	public String queryAllVideo(Model model, @RequestParam(required = true) Integer shijieId,
			@RequestParam(required = false) Integer pageNo, @RequestParam(required = false) String content,
			@RequestParam(required = false) String author, @RequestParam(required = false) String startDate,
			@RequestParam(required = false) String endDate) {
		if (pageNo == null || pageNo < 1)
			pageNo = 1;
		model.addAttribute("page", sjcs.queryAllCommentById(pageNo, shijieId, content, author, startDate, endDate));
		model.addAttribute("shijieId", shijieId);
		model.addAttribute("author", author);
		model.addAttribute("content", content);
		model.addAttribute("startDate", startDate);
		model.addAttribute("endDate", endDate);
		return "/shijiecomment_manager";
	}

	@RequestMapping(value = "/delete")
	public String deleteComment(Model model, Integer[] commentId, @RequestParam(required = true) Integer shijieId,
			@RequestParam(required = false) Integer pageNo, @RequestParam(required = false) String content,
			@RequestParam(required = false) String author, @RequestParam(required = false) String startDate,
			@RequestParam(required = false) String endDate) {
		if (shijieId != null) {
			sjcs.delete(commentId);
		} else {
			System.out.println("commentId is null");
		}
		return "forward:/shijie_comment/query";
	}

	@RequestMapping(value = "/audit")
	public String auditComment(Model model, Integer[] commentId, @RequestParam(required = true) Integer shijieId,
			@RequestParam(required = false) Integer pageNo, @RequestParam(required = false) String content,
			@RequestParam(required = false) String author, @RequestParam(required = false) String startDate,
			@RequestParam(required = false) String endDate, @RequestParam(required = true) int passVal) {
		if (shijieId != null) {
			if (passVal == 0)
				sjcs.audit(commentId, false);
			else
				sjcs.audit(commentId, true);
		}
		return "forward:/shijie_comment/query";
	}
}
